package com.hugesoft.logic.managers.trafficmanager;

import java.util.List;

import android.os.Handler;

import com.hugesoft.commonlogic.dao.DbManager;
import com.hugesoft.logic.managers.base.BaseBusinessLogicManager;
import com.hugesoft.logic.managers.common.Constans;
import com.hugesoft.logic.module.Bus;
import com.hugesoft.logic.module.BusDao;
import com.hugesoft.logic.module.Traffic;
import com.hugesoft.logic.module.TrafficDao;

import de.greenrobot.dao.query.QueryBuilder;

public class TrafficManager extends BaseBusinessLogicManager implements ITrafficManagerInterface{

	@Override
	public void addtrafficinfo(Traffic traffic, Handler handler) {
		try {
			traffic.setId(null);
			DbManager.getTrafficDao().insert(traffic);
			handleaddtrafficresponse(true,handler);
		} catch (Exception e) {
			handleaddtrafficresponse(false, handler);
		}
	}

	@Override
	public void deltrafficinfo(String platenumber, Handler handler) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void gettrafficinfo(String platenumber, Handler handler) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void gettrafficinfowithrecordid(Long id, Handler handler) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deltrafficinfowithrecordid(Long id, Handler handler) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void modifytrafficinfowithrecordid(Long id, Handler handler) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Traffic> loadtrafficinfo(String platenumber) {
		List<Traffic> ret = null;
		QueryBuilder<Traffic> qBuilder = DbManager.getTrafficDao()
				.queryBuilder();
		qBuilder.orderDesc(TrafficDao.Properties.Checkdate);
		qBuilder.where(
				TrafficDao.Properties.Platenumber.eq(platenumber));

		ret = qBuilder.list();
		
		return ret;
	}

	@Override
	public Traffic loadtrafficinfo(Long trafficid) {
		Traffic ret = null;
		QueryBuilder<Traffic> qBuilder = DbManager.getTrafficDao()
				.queryBuilder();
		qBuilder.where(
				TrafficDao.Properties.Id.eq(trafficid));
		qBuilder.orderAsc(TrafficDao.Properties.Checkdate);

		ret = qBuilder.unique();
		return ret;
	}

	@Override
	public List<Traffic> loadalltraffic() {
		List<Traffic> ret = null;
		QueryBuilder<Traffic> qBuilder = DbManager.getTrafficDao()
				.queryBuilder();
		qBuilder.orderAsc(TrafficDao.Properties.Checkdate);
		ret = qBuilder.list();
		return ret;
	}

	private void handleaddtrafficresponse(boolean isSuccess,Handler handler)
	{
		handleresponse(isSuccess?Constans.ADD_TRAFFIC_SUCCESS:Constans.ADD_TRAFFIC_FAILED, null, handler);
	}
	
}
